package com.example.fms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fms.dto.req.ActivityUserReq;
import com.example.fms.dto.resp.ActivityUserResp;
import com.example.fms.entity.Activity;
import com.example.fms.entity.ActivityUser;
import com.example.fms.entity.User;
import com.example.fms.exception.BusinessException;
import com.example.fms.exception.ExceptionEnum;
import com.example.fms.mapper.ActivityMapper;
import com.example.fms.mapper.ActivityUserMapper;
import com.example.fms.mapper.UserMapper;
import com.example.fms.service.ActivityService;
import com.example.fms.service.ActivityUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ActivityUserServiceImpl extends ServiceImpl<ActivityUserMapper, ActivityUser> implements ActivityUserService {

    @Resource
    UserMapper userMapper;

    @Resource
    ActivityMapper activityMapper;

    @Resource
    ActivityUserMapper activityUserMapper;

    @Override
    public Boolean attend(ActivityUserReq req) {
        ActivityUser activityUser = new ActivityUser();
        if (req.getUserId() != null) {
            User user = userMapper.selectById(req.getUserId());
            if (Objects.isNull(user)) {
                throw new BusinessException(ExceptionEnum.USER_IS_NOT);
            } else {
                activityUser.setActivityId(req.getActivityId());
                Activity activity = activityMapper.selectById(req.getActivityId());
                //如果
                if (activity.getState().equals("已经过期")) {
                    throw new BusinessException(ExceptionEnum.ACTIVITY_STATUS_INCORRECT);
                }
                Long number = activity.getNumber();
                long max = number + 1;
                if (max > activity.getMaxNumber()) {
                    throw new BusinessException(ExceptionEnum.MAXIMUM_NUMBER_OF_PEOPLE);
                } else  {
                    LambdaQueryWrapper<ActivityUser> qw = new LambdaQueryWrapper<>();
                    qw.eq(ActivityUser::getUserId, req.getUserId()).eq(ActivityUser::getActivityId, req.getActivityId());
//                    if (Objects.nonNull(activityUserMapper.selectOne(qw))) {
//                        throw new BusinessException(ExceptionEnum.DELETE_NUll);
//                    }
                    activity.setNumber(max);
                    activityMapper.updateById(activity);
                }
                activityUser.setUserId(req.getUserId());
                activityUser.setUserName(user.getName());
                return save(activityUser);
            }
        } else if (req.getUserName() != null) {
            LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
            qw.eq(User::getUserName, req.getUserName());
            User user = userMapper.selectOne(qw);
            if (Objects.isNull(user)) {
                throw new BusinessException(ExceptionEnum.USER_IS_NOT);
            } else {
                activityUser.setActivityId(req.getActivityId());
                Activity activity = activityMapper.selectById(req.getActivityId());
                Long number = activity.getNumber();
                long max = number + 1;
                if (max > activity.getMaxNumber()) {
                    throw new BusinessException(ExceptionEnum.MAXIMUM_NUMBER_OF_PEOPLE);
                } else {
                    activity.setNumber(max);
                    activityMapper.updateById(activity);
                }
                activityUser.setUserId(user.getId());
                activityUser.setUserName(user.getName());
                return save(activityUser);
            }
        }
        return false;
    }

    @Override
    public List<ActivityUserResp> List(ActivityUserReq req) {
        LambdaQueryWrapper<ActivityUser> qw = new LambdaQueryWrapper<>();
        qw.eq(ActivityUser::getActivityId, req.getActivityId());
        List<ActivityUser> activityUsers = activityUserMapper.selectList(qw);
        List<ActivityUserResp> activityUserRespList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(activityUsers)) {
            activityUsers.forEach(s -> {
                ActivityUserResp activityUserResp = new ActivityUserResp();
                activityUserResp.setId(s.getId());
                activityUserResp.setActivityId(s.getActivityId());
                activityUserResp.setUserId(s.getUserId());
                activityUserResp.setUserName(s.getUserName());
                User user = userMapper.selectById(s.getUserId());
                if (!Objects.isNull(user)) {
                    activityUserResp.setEmail(user.getEmail());
                    activityUserResp.setMobile(user.getMobile());
                    activityUserResp.setSex(user.getSex());
                }
                activityUserRespList.add(activityUserResp);
            });
        }
        return activityUserRespList;
    }

    @Override
    public Boolean exit(ActivityUserReq req) {
        ActivityUser activityUser = activityUserMapper.selectById(req.getId());
        Long activityId = activityUser.getActivityId();
        Activity activity = activityMapper.selectById(activityId);
        Long number = activity.getNumber();
        activity.setNumber(number - 1);
        activityMapper.updateById(activity);
        activityUserMapper.deleteById(req.getId());
        return true;
    }
}
